package com.github.pig.sales.controller;
import java.util.List;
import java.util.Map;
import java.util.Date;

import com.github.pig.admin.service.SysUserService;
import com.github.pig.common.vo.UserVO;
import com.github.pig.sales.entity.SalesCollarHis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pig.common.constant.CommonConstant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.sales.entity.SalesIntentionFlowup;
import com.github.pig.sales.service.ISalesIntentionFlowupService;
import com.github.pig.common.web.BaseController;

/**
 * <p>
 * 意向跟进 前端控制器
 * </p>
 *
 * @author andrew
 * @since 2018-06-23
 */
@RestController
@RequestMapping("/salesIntentionFlowup")
public class SalesIntentionFlowupController extends BaseController {
    @Autowired private ISalesIntentionFlowupService salesIntentionFlowupService;

    @Autowired
    private SysUserService userService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return SalesIntentionFlowup
    */
    @GetMapping("/{id}")
    public R<SalesIntentionFlowup> get(@PathVariable Integer id) {
        return new R<>(salesIntentionFlowupService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params,
                     @RequestParam(value = "params.student_id",required = false ,defaultValue = "-1") int id, UserVO userVO) {
        params.remove("total");
        if(id>=0){
            EntityWrapper<SalesIntentionFlowup> ew = new EntityWrapper<>();
            logger.debug(ew.getSqlSegment());
            params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
            return salesIntentionFlowupService.selectPage(new Query<>(params),ew);
        }else {
            params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
//            params.put("create_user",userService.findUserByUsername(userVO.getUsername()).getUserId());
            return salesIntentionFlowupService.selectPage(new Query<>(params), new EntityWrapper<>());
        }
    }

    /**
     * 添加
     * @param  salesIntentionFlowup  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody SalesIntentionFlowup salesIntentionFlowup) {
        salesIntentionFlowup.setCreateTime(new Date());
        salesIntentionFlowup.setUpdateTime(new Date());
        salesIntentionFlowup.setDelFlag(CommonConstant.STATUS_NORMAL);
        return new R<>(salesIntentionFlowupService.insert(salesIntentionFlowup));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        SalesIntentionFlowup salesIntentionFlowup = new SalesIntentionFlowup();
        salesIntentionFlowup.setFid(id);
        salesIntentionFlowup.setUpdateTime(new Date());
        salesIntentionFlowup.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(salesIntentionFlowupService.updateById(salesIntentionFlowup));
    }

    /**
     * 编辑
     * @param  salesIntentionFlowup  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody SalesIntentionFlowup salesIntentionFlowup) {
        salesIntentionFlowup.setUpdateTime(new Date());
        logger.info("edit: {}", salesIntentionFlowup);
        return new R<>(salesIntentionFlowupService.updateById(salesIntentionFlowup));
    }

    @GetMapping("/getByStuId/{studentId}")
    public List<Map<String,Object>> getByStuId(@PathVariable Integer studentId){
        return salesIntentionFlowupService.getByStuId(studentId);
    }
}
